package com.component.encrypt;

import java.util.Iterator;
import java.util.Map.Entry;

import com.component.StringUtil;

import net.sf.json.JSONObject;

/**
 * 解密工具类
 * 
 * @author AllenWu
 */
public class DecryptUtil {
	/**
	 * @param paramJson
	 * @return
	 */
	public static boolean checkSign(JSONObject paramJson) {
		try {
			paramJson.remove("playerID");
			String sign = paramJson.getString("sign");
			paramJson.remove("sign");
			StringBuffer sbuffer = new StringBuffer();
			@SuppressWarnings("unchecked")
			Iterator<Entry<String, Object>> iter = paramJson.entrySet().iterator();
			while (iter.hasNext()) {
				Entry<String, Object> entry = iter.next();
				sbuffer.append(entry.getValue());
			}
			return RSA.checkSign(sbuffer.toString().getBytes(), Base64.decode2(sign), Base64.decode2(Config.DE_PUBLICKEY));
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 签名验证
	 * 
	 * @param data
	 *            需验证的数据
	 * @param publicKey
	 *            公钥
	 * @return JSONObject
	 * @throws Exception
	 */
	public static JSONObject checkSign(String data, String publicKey) throws Exception {
		JSONObject json = JSONObject.fromObject(data);
		StringBuffer sbuffer = new StringBuffer();

		String sign = json.getString("sign");
		json.remove("sign");

		@SuppressWarnings("unchecked")
		Iterator<Entry<String, Object>> iter = json.entrySet().iterator();
		while (iter.hasNext()) {
			Entry<String, Object> entry = iter.next();
			sbuffer.append(entry.getValue());
		}
		boolean isPass = RSA.checkSign(sbuffer.toString().getBytes(), Base64.decode2(sign), Base64.decode2(publicKey));
		if (isPass)
			return json;
		else
			return null;
	}

	/**
	 * 签名验证
	 * 
	 * @param data
	 *            需验证的数据
	 * @param publicKey
	 *            公钥
	 * @param ENCODING
	 *            编码
	 * @return
	 * @throws Exception
	 */
	public static JSONObject checkSign(String data, String publicKey, String ENCODING) throws Exception {
		JSONObject json = JSONObject.fromObject(data);
		StringBuffer sbuffer = new StringBuffer();

		String sign = json.getString("sign");
		json.remove("sign");

		@SuppressWarnings("unchecked")
		Iterator<Entry<String, Object>> iter = json.entrySet().iterator();
		while (iter.hasNext()) {
			Entry<String, Object> entry = iter.next();
			sbuffer.append(entry.getValue());
		}
		boolean isPass = RSA.checkSign(sbuffer.toString().getBytes(ENCODING), Base64.decode2(sign), Base64.decode2(publicKey));
		//System.out.println(isPass);
		if (isPass)
			return json;
		else
			return null;
	}

	/**
	 * RSA解密
	 * 
	 * @param cryptograph
	 *            需要解密的数据
	 * @param privateKey
	 *            私钥
	 * @return
	 * @throws Exception
	 */
	public static String decryptRSA(String cryptograph, String privateKey) throws Exception {
		byte[] data = RSA.decrypt(Base64.decodeBase64(cryptograph.getBytes()), privateKey.getBytes(), Config.TRANSFORMATION_RSA);
		return new String(data);
	}

	/**
	 * 
	 * @param cryptograph
	 *            需要解密的数据
	 * @param privateKey
	 *            私钥
	 * @param ENCODING
	 *            编码
	 * @return
	 * @throws Exception
	 */
	public static String decryptRSA(String cryptograph, String privateKey, String ENCODING) throws Exception {
		byte[] data = RSA.decrypt(Base64.decodeBase64(cryptograph.getBytes()), privateKey.getBytes(), Config.TRANSFORMATION_RSA);
		return new String(data, ENCODING);
	}

	/**
	 * AES解密
	 * 
	 * @param cryptograph
	 *            需要解密的数据
	 * @param key
	 *            随机加密密钥
	 * @param iv
	 *            偏移密码
	 * @return
	 * @throws Exception
	 */
	public static String decryptAES(String cryptograph, String key, String iv) throws Exception {
		byte[] data;
		if (iv != null)
			data = AES.decrypt(Base64.decode(cryptograph.getBytes()), key.getBytes(), iv.getBytes());
		else
			data = AES.decrypt(Base64.decode(cryptograph.getBytes()), key.getBytes());
		return new String(data).trim();
	}

	/**
	 * AES解密
	 * 
	 * @param cryptograph
	 *            需要解密的数据
	 * @param key
	 *            随机加密密钥
	 * @param iv
	 *            偏移密码
	 * @param ENCODING
	 *            编码
	 * @return
	 * @throws Exception
	 */
	public static String decryptAES(String cryptograph, String key, String iv, String ENCODING) throws Exception {
		byte[] data;
		if (iv != null)
			data = AES.decrypt(Base64.decode(cryptograph.getBytes()), key.getBytes(), iv.getBytes());
		else
			data = AES.decrypt(Base64.decode(cryptograph.getBytes()), key.getBytes());
		return new String(data, ENCODING).trim();
	}

	public static JSONObject getValidData(JSONObject json) throws Exception {
		//System.out.println("<-----" + json.toString());
		// 需要解密
		if (json.getInt("encrptType") == 1 || json.getInt("encrptType") == 3) {
			String encryptData = json.getString("encryptData");
			String encryptKey = json.getString("encryptKey");
			String encryptIv = json.getString("encryptIv");
			String AESKey = decryptRSA(encryptKey, Config.DE_PRIVATEKEY);
			String AESIv = "";

			if (StringUtil.isNotNullAndTrim(encryptIv))
				AESIv = decryptRSA(encryptIv, Config.DE_PRIVATEKEY);
			else
				AESIv = null;

			String data = decryptAES(encryptData, AESKey, AESIv);
			return DecryptUtil.checkSign(data, Config.DE_PUBLICKEY);
		} else
			return json;
	}
}
